Welcome To
Methods
In List

Methods In Lists

The method is function that perform a some operations in the lists of python. The Python provide the methods to perform the more operations in the python code . some python lists methods in the below

Methods

  1. insert()
  2. remove()
  3. pop()
  4. append()
  5. extend()
  6. index()
  7. count()
  8. reverse()
  9. clear

Insert() Method

THe Insert() method is used to perform the some operation in the python list. it is used to insert the new element to list . and we can also set the position of the adding element in the lists.

Syntax

list.insert(index,value)

Example

list=[1,2,3,4]
list.insert(2,5)
print(list)

Output

[1, 2, 5, 3, 4]

remove() Method

THe remove() method is used to remove the existing list element in the python. Example in the below

Syntax

list.remove(value)

Example

list=[1,2,5,3,4]
list.remove(5)
print(list)


Output

[1, 2, 3, 4]

pop() method

THe pop() method also used to remove the element in the list .but it based on the index value it remove the element

Syntax

list.pop(index)

Example

list=[1,2,5,3,4]
list.pop(1)
print(list)

Output

[1, 5, 3, 4]

append() Method

The append method is used to add the new element to list but it does not takes the index value . the adding element position should be n-1 or last of the list

Syntax

list.append(value)

Example

list=[1,2,5,3,4]
list.append(6)
print(list)

Output

[1, 2, 5, 3, 4, 6]

extend() Method

The Extend() method is used to add the contents of list1 to the end of the list . the extend method modifiers the original list . it doesn't return any value.

syntax

list.extend(list1)

Example

list=[1,2,5,3,4]
list1=["hello","Darshan"]
list.extend(list1)
print(list)

Output

[1, 2, 5, 3, 4, 'hello', 'Darshan']

count() Method

THe count() method return the number of times the specified element appears in the list.

Syntax

list.count(element)

Example

list=[1,2,5,3,4,5,4,5]
print(list.count(5))

Output

3

index() Method

The index method is used to search the element from the list and the access the elements from the list by using index method and inside the method place element , start , end index . Example in the below

Syntax

list.index(element)

Example

list=[1,2,5,3,4,5,4,5]
print(list.index(2))

Output

1

reverse() Method

THe reverse() method is used to print the list elements in reverse form . the start list element last . and last element is first. understand by Example

syntax

list.reverse()

Example

list=[1,2,5,3,4,5,4,5]
list.reverse()
print( list)

Output

[5, 4, 5, 4, 3, 5, 2, 1]

clear() Method

The Clear() method is used to clear the all element in the list / remove the all elements in the list

Syntax

list.clear()

Example

list=[1,2,5,3,4,5,4,5]
list.clear()
print( list)

Output

[]